package com.woniuxy.table.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.table.common.TableResultCode;
import com.woniuxy.table.infra.mapper.DiningAreaMapper;
import com.woniuxy.table.infra.mapper.DinnerTableMapper;
import com.woniuxy.table.infra.model.DiningArea;
import com.woniuxy.table.infra.model.DinnerTable;
import com.woniuxy.table.service.DiningAreaService;
import com.woniuxy.table.service.dto.DiningAreaDto;
import com.woniuxy.table.service.param.AddDiningAreaServiceParam;
import com.woniuxy.table.service.param.LoadAllDiningAreaServiceParam;
import com.woniuxy.table.service.param.ModifyDiningAreaServiceParam;
import common.WoniuPage;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 餐桌区域表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-20
 */
@Service
public class DiningAreaServiceImpl extends ServiceImpl<DiningAreaMapper, DiningArea> implements DiningAreaService {
    @Autowired
    private DiningAreaMapper diningAreaMapper;
    @Autowired
    private DinnerTableMapper dinnerTableMapper;
    @Override
    public void addDiningArea(AddDiningAreaServiceParam addDiningAreaServiceParam) {
        QueryWrapper<DiningArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dining_area_name",addDiningAreaServiceParam.getDiningAreaName()).eq("store_id",addDiningAreaServiceParam.getStoreId());
        DiningArea diningArea = diningAreaMapper.selectOne(queryWrapper);
        if (diningArea!=null){
            throw new MallException(TableResultCode.DUPLICATE_REGIONAL_ADDRESS);
        }
        DiningArea newDiningArea = new DiningArea();
        newDiningArea.setDiningAreaName(addDiningAreaServiceParam.getDiningAreaName());
        newDiningArea.setFee(addDiningAreaServiceParam.getFee());
        newDiningArea.setState(addDiningAreaServiceParam.getState());
        newDiningArea.setStoreId(addDiningAreaServiceParam.getStoreId());
        save(newDiningArea);
    }

    @Override
    public void modifyDiningArea(ModifyDiningAreaServiceParam modifyDiningAreaServiceParam) {
        DiningArea diningArea = getById(modifyDiningAreaServiceParam.getDiningAreaId());
        if (diningArea==null){
            throw new MallException(TableResultCode.INVALID_REGIONAL_ADDRESS);
        }
        if (diningArea.getStoreId()!=modifyDiningAreaServiceParam.getStoreId()){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        DiningArea area = BeanUtil.toBean(modifyDiningAreaServiceParam, DiningArea.class);
        updateById(area);
    }

    @Override
    public void deleteDiningArea(Integer storeId, Integer diningAreaId) {
        DiningArea diningArea = getById(diningAreaId);
        if (diningArea==null){
            throw new MallException(TableResultCode.INVALID_REGIONAL_ADDRESS);
        }
        if (diningArea.getStoreId()!=storeId){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        QueryWrapper<DinnerTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dining_area_id",diningArea.getDiningAreaId());
        List<DinnerTable> dinnerTables = dinnerTableMapper.selectList(queryWrapper);
        if (dinnerTables!=null||dinnerTables.size()!=0){
            throw new MallException(TableResultCode.DELETE_FAILED);
        }

        removeById(diningAreaId);
    }

    @Override
    public WoniuPage<DiningAreaDto> loadAllDiningArea(LoadAllDiningAreaServiceParam loadAllDiningAreaServiceParam) {
        QueryWrapper<DiningArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",loadAllDiningAreaServiceParam.getStoreId());
        if (loadAllDiningAreaServiceParam.getMinFee()!=null){
            queryWrapper.ge("fee",loadAllDiningAreaServiceParam.getMinFee());
        }
        if (loadAllDiningAreaServiceParam.getMaxFee()!=null){
            queryWrapper.le("fee",loadAllDiningAreaServiceParam.getMaxFee());
        }
        if (loadAllDiningAreaServiceParam.getDiningAreaName()!=null){
            queryWrapper.like("dining_area_name",loadAllDiningAreaServiceParam.getDiningAreaName());
        }
        if (loadAllDiningAreaServiceParam.getState()!=null){
            queryWrapper.eq("state",loadAllDiningAreaServiceParam.getState());
        }
        Page<DiningArea> diningAreaPage = new Page<>(loadAllDiningAreaServiceParam.getPageIndex(), loadAllDiningAreaServiceParam.getPageSize());
        diningAreaMapper.selectPage(diningAreaPage,queryWrapper);
        WoniuPage<DiningAreaDto> woniuPage = new WoniuPage<DiningAreaDto>();
        woniuPage.setTotal(diningAreaPage.getTotal());
        woniuPage.setPages(diningAreaPage.getPages());
        woniuPage.setPageIndex(diningAreaPage.getCurrent());
        woniuPage.setPageSize(diningAreaPage.getSize());
        ArrayList<DiningAreaDto> list = new ArrayList<>();
        for (DiningArea diningArea : diningAreaPage.getRecords()) {
            DiningAreaDto diningAreaDto = BeanUtil.toBean(diningArea, DiningAreaDto.class);
            list.add(diningAreaDto);
        }
        woniuPage.setList(list);
        return woniuPage;
    }
}
